{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Doom Game"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "Before diving in, Let us familiarize ourselves with a vizdoom environment by seeing the\n",
    "basic example.\n",
    "\n",
    "Let us load the necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from vizdoom import *\n",
    "import random\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create an instance to the DoomGame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "game = DoomGame()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " As we know vizdoom provides a lot of doom scenarios, for now, let us load the basic\n",
    "scenario."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "game.load_config(\"basic.cfg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " init() method initializes the game with the scenario"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "game.init()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " Now let us define the one hot encoded actions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "shoot = [0, 0, 1]\n",
    "left = [1, 0, 0]\n",
    "right = [0, 1, 0]\n",
    "actions = [shoot, left, right]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then We set the number of episodes we want"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "no_of_episodes = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start playing the game!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(no_of_episodes):     \n",
    "    \n",
    "    # for each episode start the game\n",
    "    game.new_episode()\n",
    "    \n",
    "    # loop until the episode is over\n",
    "    while not game.is_episode_finished():\n",
    "        \n",
    "        # get the game state\n",
    "        state = game.get_state()\n",
    "        img = state.screen_buffer\n",
    "        \n",
    "        # get the game variables\n",
    "        misc = state.game_variables\n",
    "        \n",
    "        # perform some action randomly and receuve reward\n",
    "        reward = game.make_action(random.choice(actions))\n",
    "        \n",
    "        print(reward)\n",
    "    \n",
    "    # we will set some time before starting the next epiosde\n",
    "    time.sleep(2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:anaconda]",
   "language": "python",
   "name": "conda-env-anaconda-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
